import axios from "axios";
import { useUserStore } from "../stores/user";
import { unzip } from "./zip";

const http = axios.create({
  // cofig 中配置基础url
  baseURL : 'https://biquge.hope-api.top/biquge/api/',
  // baseURL : 'http://localhost:8083',
  // 配置超时时间
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json;charset=utf-8', // 设置请求头的编码为 UTF-8
    'Credential': 'biquge.hope-api.top', // 设置请求头的编码为 UTF-8
  },
})

// axios请求拦截器
http.interceptors.request.use( config => {

  // 每次请求携带token
  const token = useUserStore().token?.split('&')[0]
  if (token) {
    config.headers.Authorization = token;
  }
  return config;
}, e => Promise.reject(e))


// axios响应拦截器
http.interceptors.response.use( res => {
  const data = unzip(res.data.data)
  res.data.data = data
  return res.data
}, 
  
  e => {
  console.log('bug:',e.response.data);
  return Promise.reject(e)
})

export default http;